<template>

</template>

<script>
import $bus from '../bus/timer'

export default {
  name: 'TimerMixin',
  data() {
    return {
      timerList: [],// 定时器列表
      commonDivisor: 1,// 所有定时器时间的最大公约数
      commonMultiple: 1,// 所有定时器时间的最小公倍数
      maxTimer: 0,// 最大定时时间
      interval: null,// 定时器实例
      period: 0// 定时器执行周期
    }
  },
  watch: {
    'id': {
      handler(a,b) {
        this.timerList = []
        this.clearInterval()
      },
      deep: true
    }
  },
  methods: {
    timer(timer) {
      this.addTimer(timer)
      this.handleCommonDivisor()
      this.execute()
    },
    addTimer(timer) {
      let index = this.timerList.findIndex(item => item.key === timer.key)
      if (index === -1) this.timerList.push(timer)
    },

    handleCommonDivisor() {
      const getCommonDivisor = (a, b) => b === 0 ? a : getCommonDivisor(b, a % b)
      const getCommonMultiple = (a, b) => a * b / getCommonDivisor(a, b)

      let commonDivisor = 1, commonMultiple = 1
      let intervalList = this.timerList.reduce((r, c) => {
        r.push(c.interval)
        return r
      }, [])

      if (intervalList.length === 0) {
        commonDivisor = 1
        commonMultiple = 1
      } else if (intervalList.length === 1) {
        commonDivisor = intervalList[0]
        commonMultiple = intervalList[0]
      } else if (intervalList.length === 2) {
        commonDivisor = getCommonDivisor(intervalList[0], intervalList[1])
        commonMultiple = getCommonMultiple(intervalList[0], intervalList[1])
      } else {
        let i = 1, divisor = intervalList[0], multiple = intervalList[0]
        for (i; i < intervalList.length; i++) {
          divisor = getCommonDivisor(divisor, intervalList[i])
          multiple = getCommonMultiple(multiple, intervalList[i])
        }
        commonDivisor = divisor
        commonMultiple = multiple
      }
      this.commonDivisor = commonDivisor
      this.commonMultiple = commonMultiple

      console.log(`最大公约数: ${this.commonDivisor},最小公倍数: ${this.commonMultiple}`)
    },
    execute() {
      this.clearInterval()
      this.setInterval()
    },
    clearInterval() {
      if (this.interval) {
        clearInterval(this.interval)
        this.interval = null
      }
    },
    setInterval() {
      if (this.timerList.length > 0) {
        this.period = 1
        this.interval = setInterval(() => {
          console.log(`周期: ${this.commonMultiple}, 本次: ${this.period * this.commonDivisor}`)

          this.handleChartDataUpdate()

          if (this.period * this.commonDivisor >= this.commonMultiple) {
            this.period = 1
          } else {
            this.period++
          }
        }, this.commonDivisor * 1000)
      }
    },
    handleChartDataUpdate() {
      const interval = this.period * this.commonDivisor
      let pending = this.timerList.filter(item => interval % item.interval === 0)
      if (pending.length) {
        pending.forEach(item => {
          console.log('当前刷新的图表key: ', item.key)
          $bus.$emit('updateChartData', item.key)
        })
      }
    }
  },
  beforeDestroy() {
    this.timerList = []
    this.clearInterval()
    $bus.$off('handle-timer')
  },
  mounted() {
    this.timerList = []
    this.clearInterval()
    $bus.$on('handle-timer', (timer) => {
      this.timer(timer)
    })
  }
}
</script>
